


clear
capture log close
set more off


* from recs data get changes in consumption
clear

local yrover 1990 1993 1997 2001 2005 2009 2015 

foreach i of local yrover  {

use ccrecs_`i'.dta

capture drop if kwh==999999
drop if kwh==0

quietly bysort hhfaminc_1990: egen sumhh=total(nweight)
quietly bysort hhfaminc_1990: gen weight=nweight/sumhh
quietly bysort hhfaminc_1990: egen meankwh=total(weight*kwh)

quietly bysort hhfaminc_1990: egen meanincome=total(weight*ggincome)

collapse meankwh meanincome,by(hhfaminc_1990)

drop if hhfaminc_1990==.

save conspchange_`i'.dta,replace

}


******* delta e* S using recs every five year



clear
local yrover 1990 1993 1997 2001 2005 2009 2015 
 
foreach i of local yrover  {
clear
use recs_1990

capture drop if kwh==999999

qui drop if moneypy==.

qui egen sumhh=total(nweight)
qui gen weight=nweight/sumhh

collapse (sum) weight,by(moneypy) // former uses mean weight, wrong, now changes to sum of weight, right now
qui drop if moneypy==.


qui rename moneypy hhfaminc_1990
quietly merge m:1 hhfaminc_1990 using  conspchange_`i'.dta  //former named ratio.dta, not apporiate

quietly drop if hhfaminc_1990==.


quietly sum meankwh[aweight=weight]

quietly return list
dis r(mean)

}


** delta s* deltaE


clear
local yrover 1990 1993 1997 2001 2005 2009 2015 
 
foreach i of local yrover  {
clear
use ccrecs_`i'.dta,clear
capture drop if kwh==999999
qui drop if moneypy==.
qui egen sumhh=total(nweight)
qui gen weight=nweight/sumhh
collapse (sum) weight,by(hhfaminc_1990)
save weight_`i',replace
}


**

local yrover 1990 1993 1997 2001 2005 2009 2015 
foreach i of local yrover  {
clear
use recs_1990
capture drop if kwh==999999

quietly bysort moneypy: egen sumhh=total(nweight)
quietly bysort moneypy: gen weight=nweight/sumhh
quietly bysort moneypy: egen meankwh_90=total(weight*kwh)

collapse meankwh_90,by(moneypy)

quietly drop if moneypy==. 
rename moneypy hhfaminc_1990


quietly merge 1:1 hhfaminc_1990 using  weight_`i' //changes in shares

drop _merge


quietly merge m:1 hhfaminc_1990 using  conspchange_`i'.dta  //changes in consumption 

quietly drop if hhfaminc_1990==.


quietly sum meankwh[aweight=weight]

quietly return list
dis r(mean)

}




** counterfactual 

local yrover 1990 1993 1997 2001 2005 2009 2015 
foreach i of local yrover  {
clear
use recs_1990
capture drop if kwh==999999

quietly bysort moneypy: egen sumhh=total(nweight)
quietly bysort moneypy: gen weight=nweight/sumhh
quietly bysort moneypy: egen meankwh=total(weight*kwh)

collapse meankwh,by(moneypy)

quietly drop if moneypy==. 
rename moneypy hhfaminc_1990
sort hhfaminc_1990 
quietly destring hhfaminc_1990,replace

quietly merge 1:1 hhfaminc_1990 using  weight_`i'

quietly drop if meankwh==.

quietly sum meankwh[aweight=weight]
quietly return list
dis r(mean)

}






